package com.glsc.ngateway.common.base.converter.platform;

import com.glsc.ngateway.common.base.domain.mysql.gateway.platform.it.networkassets.dict.ItNetworkAssetsDict;
import com.glsc.ngateway.common.base.domain.mysql.gateway.platform.it.networkassets.dict.ItNetworkAssetsDictField;
import com.glsc.ngateway.common.base.domain.mysql.gateway.platform.it.networkassets.dict.ItNetworkAssetsDictFieldValue;
import com.glsc.ngateway.common.api.platform.dto.it.networkassets.dict.ItNetworkAssetsDictDto;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;

public class ItNetworkAssetsDictConverter {

    public static ItNetworkAssetsDictDto do2Dto(ItNetworkAssetsDict dictDo) {
        if (Objects.isNull(dictDo)) {
            return null;
        }
        Map<String, ItNetworkAssetsDictField> fieldMap = CollectionUtils.isEmpty(dictDo.getFieldList()) ? java.util.Collections.EMPTY_MAP
                :dictDo.getFieldList().stream().collect(Collectors.toMap(ItNetworkAssetsDictField::getCode, o -> o));
        return ItNetworkAssetsDictDto.builder()
                .id(dictDo.getId())
                .code(dictDo.getCode())
                .desc(dictDo.getDescribtion())
                .level(dictDo.getLevel())
                .parentCode(dictDo.getParentCode())
                .labelField(dictDo.getLabelField())
                .valueField(dictDo.getValueField())
                .children(CollectionUtils.isEmpty(dictDo.getChildren()) ? null : dictDo.getChildren().stream().map(ItNetworkAssetsDictConverter::do2Dto).collect(Collectors.toList()))
                .fieldList(CollectionUtils.isEmpty(dictDo.getFieldList()) ? null : dictDo.getFieldList().stream().map(ItNetworkAssetsDictFieldConverter::do2Dto).collect(Collectors.toList()))
                .valueList(CollectionUtils.isEmpty(dictDo.getValueList()) ? null :
                        //将value展开并合并到一个list中
                        dictDo.getValueList()
                                .stream()
                                //根据record分组
                                .collect(Collectors.groupingBy(ItNetworkAssetsDictFieldValue::getRecordId, Collectors.toList()))
                                //转成record记录对象(Map)
                                .values()
                                .stream()
                                .map(valueList -> new HashMap<String, String>() {{
                                    AtomicReference<Long> id = new AtomicReference<>();
                                    //字段
                                    valueList.forEach(v -> {
                                        if (fieldMap.containsKey(v.getDictFieldCode())) {
                                            if(id.get() == null) {
                                                id.set(v.getRecordId());
                                            }
                                            put(fieldMap.get(v.getDictFieldCode()).getCode(), v.getValue());
                                        }
                                    });
                                    put("id", String.valueOf(id.get()));
                                }})
                                .collect(Collectors.toList())
                )
                .build();
    }

    public static ItNetworkAssetsDict dto2Do(ItNetworkAssetsDictDto dictDto) {
        return Objects.isNull(dictDto) ? null : ItNetworkAssetsDict.builder()
                .id(dictDto.getId())
                .code(dictDto.getCode())
                .describtion(dictDto.getDesc())
                .parentCode(dictDto.getParentCode())
                .labelField(dictDto.getLabelField())
                .valueField(dictDto.getValueField())
                //转Do对象一般用于写入数据库，此联级属性先不做转换
//                .level(dictDto.getLevel())
//                .children(CollectionUtils.isEmpty(dictDto.getChildren()) ? java.util.CollectionUtils.EMPTY_LIST : dictDto.getChildren().stream().map(ItNetworkAssetsDictConverter::dto2Do).collect(Collectors.toList()))
//                .fieldList(CollectionUtils.isEmpty(dictDto.getFieldList()) ? java.util.CollectionUtils.EMPTY_LIST : dictDto.getFieldList().stream().map(ItNetworkAssetsDictFieldConverter::dto2Do).collect(Collectors.toList()))
                .build();
    }

    public static List<ItNetworkAssetsDictDto> do2Dto(List<ItNetworkAssetsDict> dictDoList) {
        return CollectionUtils.isEmpty(dictDoList) ? java.util.Collections.EMPTY_LIST
                : dictDoList.stream().map(ItNetworkAssetsDictConverter::do2Dto).collect(Collectors.toList());
    }
}
